package com.group6.exam.teacher.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.UserInfoEntity;
import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.common.util.DBUtil;
import com.group6.exam.student.entity.StudentEntity;

public class TeacherDaoImpl implements ITeacherDao {

	@Override
	public boolean login(UserInfoEntity user) {
		String sql = "SELECT 1 FROM userInfo WHERE account=? AND password=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql,
				user.getAccount(), user.getPassword());

		return CollectionUtil.isEmpty(list);
	}

	@Override
	public List<Map<String, Object>> getMainMenus(String username) {
		String sql = "select * from rightInfo where "
				+ "right_id in (select right_id from roleRightInfo where "
				+ "role_id=(select role_id from userinfo where account=?)) "
				+ "and parent_id='root'";

		return DBUtil.executeQuery(sql, username);
	}

	@Override
	public List<Map<String, Object>> getChildMenus(String username) {
		String sql = "select * from rightInfo where "
				+ "right_id in (select right_id from roleRightInfo where "
				+ "role_id=(select role_id from userinfo where account=?)) "
				+ "and parent_id<>'root'";

		return DBUtil.executeQuery(sql, username);
	}

	/**
	 * 修改前查询学生信息
	 */
	public List<Map<String, Object>> getStudentPage(PageModel pageModel, String gradeName) {

		List<Object> paramList = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from studentInfo");
		
		// 条件查询
		if (gradeName != null && !"".equals(gradeName)) {
			gradeName = "%" + gradeName + "%";
			String sql = "select * from grade where grade_name like ?";
			Map<String, Object> gradeId = DBUtil.executeQuery(sql, gradeName).get(0);
			sb.append(" where grade_id=?");
			paramList.add(gradeId.get("grade_id"));
		}
		sb.append(" limit ?, ?");
		
		// 获取每页显示的第一条记录
		int index = (pageModel.getCurrentPage() - 1) * pageModel.getPageSize();
		int pageSize = pageModel.getPageSize();
		
		paramList.add(index);
		paramList.add(pageSize);
		Object[] parameters = paramList.toArray();
		return DBUtil.executeQuery(sb.toString(), parameters);
	}

	/**
	 * 获取总记录数
	 */
	public int getTotalRecords(String gradeName) {
		
		List<Object> paramList = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from studentInfo");
		
		// 条件查询
		if (gradeName != null && !"".equals(gradeName)) {
			gradeName = "%" + gradeName + "%";
			String sql = "select * from grade where grade_name like ?";
			Map<String, Object> gradeId = DBUtil.executeQuery(sql, gradeName).get(0);
			sb.append(" where grade_id=?");
			paramList.add(gradeId.get("grade_id"));
		}
		Object[] parameters = paramList.toArray();
		return DBUtil.executeQuery(sb.toString(), parameters).size();
	}
	
	/**
	 * 删除学生信息
	 */
	public int delete(int studentId) {
		String sql = "delete from studentInfo where student_id=?";
		return DBUtil.executeUpdate(sql, studentId);
	}

	/**
	 * 添加学生
	 */
	public int addStudent(StudentEntity student) {
		String sql = "insert into studentinfo values(null,?,?,?,3,123456,?,null)";
		
		Object[] parameters = { 
				student.getStudentSno(), 
				student.getAccount(),
				student.getGradeId(), 
				student.getSex() 
		};
		
		return DBUtil.executeUpdate(sql, parameters);
	}

	/**
	 * 查询年级
	 */
	public List<Map<String, Object>> selectGrade() {
		String sql = "select * from grade";
		return DBUtil.executeQuery(sql);
	}

	/**
	 * 通过ID查询学生信息
	 */
	public Map<String, Object> selectStudentById(int studentId) {
		String sql = "select * from studentInfo where student_id=?";
		
		return DBUtil.executeQuery(sql, studentId).get(0);
	}

	/**
	 * 修改学生信息
	 */
	public int update(StudentEntity student) {
		String sql = "update studentInfo set student_sno=?, student_name=?, grade_id=?, student_sex=? where student_id=?";
		Object[] parameters = {
			student.getStudentSno(),
			student.getAccount(),
			student.getGradeId(),
			student.getSex(),
			student.getId()
		};
		return DBUtil.executeUpdate(sql, parameters);
	}

}
